﻿function T_String()
{
    this.getControls = function (field, formName, value, className, customFieldName, width, notCustom)
    {
        if ($.type(value) === "number")
            value = round(value, 2);
        customFieldName = notCustom? "" : "." + (customFieldName || "String");
        var str = stringFormat("\
                        <input type='hidden' value='{0}' name='{2}.FieldName' id='{2}.FieldName'/>\
                        <input type='text' id='{2}{4}' name='{2}{4}' class='{3}'/>",
                        [field, value, formName, className || "", customFieldName]);
        var el = $(str);
        el.filter("[type='text']").val(value);
        return el;
    };

    this.getText = function (value)
    {
        if ($.type(value) === "number")
            value = round(value, 2);
        return value;
    };
}

function T_Boolean()
{
    this.getControls = function (field, formName, value, className)
    {
        return stringFormat("\
                        <input type='hidden' value='{0}', name='{2}.FieldName' id='{2}.FieldName'/>\
                        <input type='checkbox' id='{2}.Boolean' name='{2}.Boolean' {3} value='true' class='{4}'/>",
                        [field, value, formName, value ? "checked='checked'" : "", className]);
    };
}

function T_Integer()
{
    this.getControls = function (field, formName, value, className)
    {
        return new T_String().getControls(field, formName, value, "Numeric "+ className, "Integer");
    }
}

var T_Long = T_Integer;
function T_Double()
{
    this.getControls = function (field, formName, value, className, width, notCustom)
    {
        return new T_String().getControls(field, formName, value, "Numeric " + className, "Double", width, notCustom);
    }

    this.getText = function (value)
    {
        return Math.round(value);
    };
} 

function T_Date()
{
    this.getControls = function (field, formName, value, className)
    {
        if (value)
            var value = new Date(parseInt(value.substr(6))).stringify();

        return new T_String().getControls(field, formName, value, "Date " + className, "Date");
    }
}

var T_DateTime = T_Date;

function T_Duration()
{
    this.getControls = function (field, formName, value, className, type, width, notCustom)
    {
        var unitOptions = type ? currencyUnits : durationUnits;
        type = type || "Unit";

        unitOptions.value = value;
        var options = $.jqote("<option value='<%=j%>' <%= data.value && j == data.value.Unit? 'selected=\\'selected\\'': '' %> ><%=this%></option>", unitOptions);

        return stringFormat("<input type='hidden' value='{0}' name='{1}.FieldName' id='{1}.FieldName'/>\
                             <input type='text' id='{1}{7}' name='{1}{7}' class='Numeric Duration {5}' value='{4}'/>\
                             <select id='{1}.{2}' name='{1}.{2}' class='{5} Duration ddl-menu' {6}>{3}</select>"
                                 , field
                                 , formName
                                 , type
                                 , options
                                 , value ? round(value.Value, 2) : ""
                                 , className
                                 , width ? stringFormat("style='width:{0}px'", width) : ""
                                 , notCustom ? ".Value" : ".Double");
    };

    this.getText = function (value)
    {
        return value ? stringFormat("{0} {1}", round(value.Value, 2), durationUnits[value.Unit]) : "";
    };
}

function T_Money()
{
    this.getControls = function (field, formName, value, className)
    {
        return new T_Duration().getControls(field, formName, value, className, "Currency");
    };
}

function T_Entity()
{
    this.getControls = function (field, formName, value, className, ph2, typeName)
    {
        var option = value?  stringFormat("<option value='{0}'>{1}</option>", value.Id.Value, value.Name): "";
            
        return stringFormat("\
                        <input type='hidden' value='{0}', name='{1}.FieldName' id='{1}.FieldName' />\
                        <input type='hidden' value='{3}', name='{1}.TypeName' id='{1}.TypeName'  />\
                        <select id='{1}.Value' name='{1}.ID' class='EntitySearch {4}' type_name='{3}'>{2}</select>",
                        [field, formName, option, typeName, className]);
    };
}
